package com.msa.msaportal.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.SQLQuery;

import com.msa.msaportal.common.hibernate.Finder;
import com.msa.msaportal.common.hibernate.HibernateBaseDao;
import com.msa.msaportal.dao.IPortalPageDao;
import com.msa.msaportal.entity.PortalExample;
import com.msa.msaportal.entity.PortalPage;
import com.msa.msaportal.entity.PortletEntity;
import com.msa.msaportal.entity.PortletViewRes;

public class PortalPageDaoImpl extends HibernateBaseDao<PortalPage, Integer> implements IPortalPageDao {

	public PortalPage addPage(PortalPage portalPage) {

		getSession().save(portalPage);
		
		return portalPage;
	}


	@Override
	public PortletEntity addPortlet(PortletEntity portletEntity) {

		getSession().save(portletEntity);
		
		return portletEntity;
	}

	@Override
	public PortalPage findById(Integer pageid) {
		
		PortalPage entity = (PortalPage) this.getSession().get(PortalPage.class, pageid);
		return entity;
	}
	

	@Override
	public void updatePage(PortalPage portalPage) {
		
		this.getSession().update(portalPage);
	}
	

	@SuppressWarnings("unchecked")
	@Override
	public List<PortletEntity> getPortletlistBypageId(Integer pageid) {
		Finder finder = Finder.create();
		String hql = "from PortletEntity bean where bean.portalPage.pageId=:pageId";
		finder.append(hql);
		finder.setParam("pageId", pageid);
		finder.append(" order by portletorder ");
		
		return find(finder);
	}

	@Override
	public void deletePortelt(PortletEntity portlet) {
		
		getSession().delete(portlet);
	}
	

	@Override
	public PortletEntity getPortletById(Integer portletId) {
		PortletEntity entity = (PortletEntity) this.getSession().get(PortletEntity.class, portletId);
		return entity;
	}
	

	@Override
	public void updatePortlet(PortletEntity portletEntity) {
		// TODO Auto-generated method stub
		this.getSession().update(portletEntity);
	}
	
	@Override
	protected Class<PortalPage> getEntityClass() {
		// TODO Auto-generated method stub
		return null;
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<PortalPage> getPagelistByPortal(PortalExample portalExample) {
		Finder finder = Finder.create();
		String hql = "from PortalPage bean where bean.portalExample.portal_id=:portalId";
		finder.append(hql);
		finder.setParam("portalId", portalExample.getPortal_id());
		
		return find(finder);
	}


	@Override
	public PortalPage findPortalId(Integer portal_id) {
		String hql=" from PortalPage where   portal_id=? ";
		Integer id = Integer.valueOf(portal_id);
		PortalPage portalPage=(PortalPage)super.findUnique(hql, id);		
		return portalPage;
	}


	@Override
	public PortletViewRes getPortletResByid(Integer viewId) {
		PortletViewRes portletres = (PortletViewRes) this.getSession().get(PortletViewRes.class, viewId);
		return portletres;
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<PortletEntity> getPortletlistByRoleId(Integer pageId) {				
		Finder finder = Finder.create();
		String hql = "from PortletEntity  bean ";					
		finder.append(hql);
		finder.append(" where bean.portalPage.pageId=:pageId");
		finder.setParam("pageId", pageId);
		finder.append(" order by portletorder ");
		return find(finder);
	}

	
	
	@SuppressWarnings("unchecked")
	@Override
	public List<PortletEntity> getByRoleId(Integer pageId,
			Integer role_id) {				
		Finder finder = Finder.create();
		String hql = "from PortletEntity   bean join fetch   bean.portalRole s";					
		finder.append(hql);	
		finder.append(" where bean.portalPage.pageId=:pageId");
		finder.setParam("pageId", pageId);
		finder.append("  and  s.role_id=:role_id");
		finder.setParam("role_id", role_id);
		finder.append(" order by bean.portletorder ");

		return find(finder);
	}


}
